package cn.edu.huas.shopping.service;

import cn.edu.huas.shopping.dao.UserDao;
import cn.edu.huas.shopping.dao.UserDaoImpl;
import cn.edu.huas.shopping.exception.DaoException;
import cn.edu.huas.shopping.exception.UsernameExitsException;
import cn.edu.huas.shopping.exception.UsernameOrPwdErrorException;
import cn.edu.huas.shopping.pojo.User;

import java.util.Objects;

public class UserServiceImpl implements UserService{
    private UserDao userDao=new UserDaoImpl();
    @Override
    public User login(String username, String pwd) throws DaoException, UsernameOrPwdErrorException {
        User user=userDao.queryByUsernameAndPwd(username,pwd);
        if (Objects.isNull(user)) {
            throw new UsernameOrPwdErrorException("用户名或者用户密码错误");
        }
        return user;
    }

    @Override
    public void regist(User user) throws DaoException, UsernameExitsException {
        userDao.insertUser(user);
    }

    @Override
    public void checkUserName(String username) throws DaoException, UsernameExitsException {
        User user=userDao.queryByUsername(username);
        if (Objects.nonNull(user)) {
            throw  new UsernameExitsException("用户名已经存在");
        }
    }
}
